import RPi.GPIO as GPIO
import dht11
import time

# initialize GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.cleanup()
# read data using pin 16
instance = dht11.DHT11(pin = 16)
while True:
    try:
        result = instance.read()

        if result.is_valid():
            print("Temperature: %-3.1f C" % result.temperature)
            print("Humidity: %-3.1f %%" % result.humidity)
            time.sleep(1)    
        else:
            print("Error: %d" % result.error_code)
    except KeyboardInterrupt:
        # 使用结束，释放引脚
        GPIO.cleanup()
        break        
